﻿using System;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web.UI;
using System.Collections;
using Microsoft.SharePoint.WebControls;

namespace TPG.SharePoint.Workflow.Controls.Design
{
    [SupportsPreviewControl(true)]
    public class ButtonSectionDesigner
        : ControlDesigner
    {
        protected override bool UsePreviewControl
        {
            get { return true; }
        }

        protected ButtonSection Section
        {
            get { return (ButtonSection)Component; }
        }

        protected ButtonSection ViewSection
        {
            get { return (ButtonSection)ViewControl; }
        }

        public override TemplateGroupCollection TemplateGroups
        {
            get
            {
                TemplateGroupCollection groups = base.TemplateGroups;
                TemplateGroup group = new TemplateGroup("Buttons");
                group.AddTemplateDefinition(
                    new TemplateDefinition(this, "ButtonTemplate", Component, "ButtonTemplate"));
                groups.Add(group);
                return groups;
            }
        }

        public override DesignerActionListCollection ActionLists
        {
            get
            {
                DesignerActionListCollection lists = base.ActionLists;
                lists.Add(new ButtonSectionDesignerActionList(this));
                return lists;
            }
        }

        public override void Initialize(IComponent component)
        {
            ButtonSection section = component as ButtonSection;
            if (section == null)
            {
                throw new ArgumentException();
            }
            base.Initialize(component);
            SetViewFlags(ViewFlags.TemplateEditing, true);
        }

        public override string GetDesignTimeHtml(DesignerRegionCollection regions)
        {
            if (ViewSection.ButtonTemplate != null)
            {
                regions.Add(
                    new TemplatedEditableDesignerRegion(
                        new TemplateDefinition(
                            this, "ButtonTemplate", Component, "ButtonTemplate")));
            }
            return base.GetDesignTimeHtml(regions);
        }

        public override string GetDesignTimeHtml()
        {
            ButtonSection section = ViewSection;
            ((ICompositeControlDesignerAccessor)section).RecreateChildControls();
            IDictionary state = ((IControlDesignerAccessor)section).GetDesignModeState();
            if (section.ButtonTemplate != null)
            {
                ButtonSection.RepeatedControls container = (ButtonSection.RepeatedControls)state["ButtonTemplateContainer"];
                container.Controls.Clear();
                container.Attributes.Add(DesignerRegion.DesignerRegionAttributeName, "0");
                container.SeparatorHtml = @"<span style=""width:6px;""></span>";
            }
            section._defaultCancelButton.Text = "Cancel";
            SPDesignTimeStyleManager.ApplySharePointStyleRecursive(section);
            section._sectionLineRow.Visible = section.ShowSectionLine;
            section._sectionLineCell.Height = Unit.Pixel(section.SectionLineHeight);
            
            return base.GetDesignTimeHtml();
        }

        public override string GetEditableDesignerRegionContent(EditableDesignerRegion region)
        {
            IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
            return DesignerRegionHelper.GetTemplatedEditableDesignerRegionContent(
                host, (TemplatedEditableDesignerRegion)region);
        }

        public override void SetEditableDesignerRegionContent(
            EditableDesignerRegion region, string content)
        {
            IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
            DesignerRegionHelper.SetTemplatedEditableDesignerRegionContent(
                host, (TemplatedEditableDesignerRegion)region, content);
            ViewControlCreated = false;
        }

        void ConvertToButtonTemplate()
        {
            IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
            DesignerRegionHelper.ConvertToTemplate(
                host, "ButtonTemplate", Section, "ButtonTemplateContainer");
            ViewControlCreated = false;
        }

        void ResetButtonTemplate()
        {
            DesignerRegionHelper.ResetTemplate("ButtonTemplate", Section);
        }

        class ButtonSectionDesignerActionList
            : DesignerActionList
        {
            ButtonSectionDesigner _designer;

            public ButtonSectionDesignerActionList(ButtonSectionDesigner designer)
                : base(designer.Component)
            {
                _designer = designer;
            }

            public override DesignerActionItemCollection GetSortedActionItems()
            {
                DesignerActionItemCollection items = new DesignerActionItemCollection();
                if (_designer.InTemplateMode == false)
                {
                    if (_designer.ViewSection.ButtonTemplate == null)
                    {
                        items.Add(
                            new DesignerActionMethodItem(
                                this, "ConvertToButtonTemplate", "Convert to button template", true));
                    }
                    else
                    {
                        items.Add(
                            new DesignerActionMethodItem(
                                this, "ResetButtonTemplate", "Reset button template", true));
                    }
                }
#if DEBUG
                items.Add(new DesignerActionMethodItem(
                    this, "GetHtml", "Get Html"));
#endif
                return items;
            }
#if DEBUG
            public void GetHtml()
            {
                HtmlPreviewer.ShowForDesigner(_designer);
            }
#endif

            public void ConvertToButtonTemplate()
            {
                _designer.ConvertToButtonTemplate();
            }

            public void ResetButtonTemplate()
            {
                _designer.ResetButtonTemplate();
            }
        }
    }
}
